package henu.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import henu.IF.ManagementMessageIF;
import henu.bean.Message;
import henu.others.subject;
import henu.util.DbcpPool;

public class ManagementMessageImpl implements ManagementMessageIF{
	
	

	@Override
	public boolean addMessage(Message message) {
		String sql = "insert into Message(sid,tid,content,reply) values(?,?,?,?)";
		PreparedStatement ps = DbcpPool.executePreparedStatement(sql);
		int  r=0;
		try{
			ps.setString(1, message.getSid());
			ps.setString(2, message.getTid());
			ps.setString(3, message.getContent());
			ps.setString(4, null);
			r=ps.executeUpdate();
			ps.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		DbcpPool.close();
		if(r==0)
			return false;
		else 
			return true;
	}

	@Override
	public List<Message> findSendMessage(String sid) {
		// TODO Auto-generated method stub
		
		String sql = "select a.*,b.tname from message a inner join teacher b on a.tid = b.tid where a.sid='"+sid+"'";
		List<Message> list = new ArrayList<Message>();
		ResultSet rs = null;
        String sb = "";
		try{
			rs = DbcpPool.executeQuery(sql);
			while(rs.next()){
			Message message = new Message();
			message.setMid(rs.getInt("mid"));
			message.setSid(rs.getString("sid"));
			message.setTid(rs.getString("tid"));
			message.setContent(rs.getString("content"));
			message.setReply(rs.getString("reply"));
			message.setTname(rs.getString("tname"));
			list.add(message);
			}
			rs.close();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		DbcpPool.close();
		return list;
	}

	@Override
	public List<Message> findAcceptMessage(String tid) {
		// TODO Auto-generated method stub
		
				String sql = "select a.*,b.sname from message a inner join student b on a.sid = b.sid where a.tid='"+tid+"'";
				List<Message> list = new ArrayList<Message>();
				ResultSet rs = null;
		        String sb = "";
				try{
					rs = DbcpPool.executeQuery(sql);
					while(rs.next()){
					Message message = new Message();
					message.setMid(rs.getInt("mid"));
					message.setSid(rs.getString("sid"));
					message.setTid(rs.getString("tid"));
					message.setContent(rs.getString("content"));
					message.setReply(rs.getString("reply"));
					message.setSname(rs.getString("sname"));
					list.add(message);
					}
					rs.close();
				}catch(Exception e)
				{
					e.printStackTrace();
				}
				DbcpPool.close();
				return list;
	}

	@Override
	public boolean deleteMessage(int mid) {
		String sql = "DELETE FROM message WHERE mid= " + mid + "";
		int result = 0 ;
		result = DbcpPool.executeUpdate(sql);
		DbcpPool.close();
		if(result>0)
			return true;
		else
			return false;
	}

	@Override
	public boolean updateMessage(Message message) {
		String sql = null;
		sql = "update message set reply=? where mid=?";
	    PreparedStatement ps = DbcpPool.executePreparedStatement(sql);
	    int result = 0;
	    try {
		
		ps.setString(1, message.getReply());
		ps.setInt(2, message.getMid());
		result = ps.executeUpdate();
		ps.close();
	    } catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    DbcpPool.close();
	   
	   if(result>0)
	       return true;
	   else 
	       return false;
	}

}
